﻿using MesErreurs;
using MySql.Data.MySqlClient;
using Persistance;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Metier
{
    public class DetailCde
    {
        //Definition des attributs
        private string noCommande;
        private string noArticle;
        private int qteCommandee;
        private string livree;

        public string Livree
        {
            get { return livree; }
            set { livree = value; }
        }

        public int QteCommandee
        {
            get { return qteCommandee; }
            set { qteCommandee = value; }
        }

        public string NoArticle
        {
            get { return noArticle; }
            set { noArticle = value; }
        }

        public string NoCommande
        {
            get { return noCommande; }
            set { noCommande = value; }
        }

        public DetailCde()
        {
            noCommande = "";
            noArticle = "";
            qteCommandee = 0;
            livree = "";
        }
                
        public DetailCde (string noCom, string noArt, int qteCom, string liv)
        {
            noCommande = noCom;
            noArticle = noArt;
            qteCommandee = qteCom;
            livree = liv;
        }


        //Traitement de la classe DetailCde
        /// <summary>
        /// Lire un utilisateur sur son ID
        /// </summary>
        /// <param name="id_utilisateur">N° de l'utilisateur à lire</param>
        public List<String> LectureDetailCommande()
        {
            List<String> mesNumeros = new List<String>();
            DataTable dt;
            sErreurs er = new sErreurs("Erreur sur lecture du détail de la commande.", "Utilisateur.LectureDetailCommande()");
            try
            {
                String mysql = "SELECT DISTINCT NO_COMMAND, NO_ARTICLE FROM DETAIL_CDE ORDER BY NO_COMMAND";
                dt = DbInterface.Lecture(mysql, er);
                foreach (DataRow dataRow in dt.Rows)
                {
                    mesNumeros.Add((dataRow[0]).ToString());
                }
                return mesNumeros;
            }
            catch (MySqlException e)
            {
                throw new MonException(er.MessageUtilisateur(), er.MessageApplication(),
                e.Message);
            }
        }

        public DetailCde RechercheUnDetail(String numCom, String numArt)
        {
            String mysql;
            DataTable dt;
            sErreurs er = new sErreurs("Erreur sur recherche d'un détail de commande.", "Client.RechercheUnDetail()");
            try
            {
                mysql = "SELECT NO_COMMAND, NO_ARTICLE, QTE_CDEE,";
                mysql += "LIVREE ";
                mysql += "FROM DETAIL_CDE WHERE NO_COMMAND=':numero' AND  NO_ARTICLE=':champ'";
                // On se protège de l’injection sql
                mysql = mysql.Replace(":numero", numCom);
                mysql = mysql.Replace(":champ", numArt);
                // On appelle la couche Persistance
                dt = DbInterface.Lecture(mysql, er);
                if (dt.IsInitialized && dt.Rows.Count!=0)
                {
                    DataRow dataRow = dt.Rows[0];
                    this.noCommande = numCom;
                    this.noArticle = numArt;
                    this.qteCommandee = (int) dataRow[2];
                    this.livree = dataRow[3].ToString();
                    return this;
                }
                else return new DetailCde();
            }
            catch (MySqlException e)
            {
                throw new MonException(er.MessageUtilisateur(), er.MessageApplication(),
                e.Message);
            }
        }

        public List<Article> getLesArticles(string numCom)
        {
            DataTable dt;
            sErreurs er = new sErreurs("Erreur sur recherche d'une commande.", "Commande.getLesArticles()");
            String mysql = "SELECT NO_ARTICLE ";
            mysql += "FROM DETAIL_CDE WHERE NO_COMMAND=':numero'";
            mysql = mysql.Replace(":numero", numCom);

            try
            {
                dt = DbInterface.Lecture(mysql, er);
                List<Article> mesArt = new List<Article>();
                foreach (DataRow dataRow in dt.Rows)
                {
                    Article unart = new Article();
                    unart.RechercheUnArticle(dataRow[0].ToString());
                    mesArt.Add(unart);
                }
                return mesArt;
            }
            catch (MonException erreur)
            {
                throw erreur;
            }
        }

        public void insererDetail()
        {
            String requete = "INSERT INTO DETAIL_CDE VALUES (':numCom',':numArt',:quantite,':livree');";
            requete = requete.Replace(":numCom", noCommande);
            requete = requete.Replace(":numArt", noArticle);
            requete = requete.Replace(":quantite", qteCommandee.ToString());
            requete = requete.Replace(":livree", livree);

            sErreurs er = new sErreurs("Erreur sur l'insertion du detail de commande.", "Utilisateur.insererDetail");
            try
            {
                DbInterface.Ecriture(requete,"INSERT");
            }
            catch (MySqlException e)
            {
                throw new MonException(er.MessageUtilisateur(), er.MessageApplication(),
                e.Message);
            }
        }

        public void updateDetail()
        {
            String requete = "UPDATE DETAIL_CDE SET QTE_CDEE = :quantite, LIVREE=':livree' WHERE NO_COMMAND=':numCom'AND NO_ARTICLE=':numArt';";
            requete = requete.Replace(":numCom", noCommande);
            requete = requete.Replace(":numArt", noArticle);
            requete = requete.Replace(":quantite", qteCommandee.ToString());
            requete = requete.Replace(":livree", livree);

            sErreurs er = new sErreurs("Erreur sur la modification du detail de commande.", "Utilisateur.updateDetail");
            try
            {
                DbInterface.Ecriture(requete,"UPDATE");
            }
            catch (MySqlException e)
            {
                throw new MonException(er.MessageUtilisateur(), er.MessageApplication(),
                e.Message);
            }
        }

        public void supprimerDetail()
        {
            String requete = "DELETE FROM DETAIL_CDE WHERE NO_COMMAND=':numCom'AND NO_ARTICLE=':numArt';";
            requete = requete.Replace(":numCom", noCommande);
            requete = requete.Replace(":numArt", noArticle);

            sErreurs er = new sErreurs("Erreur sur la suppression du detail de commande.", "Utilisateur.supprimerDetail");
            try
            {
                DbInterface.Ecriture(requete, "DELETE");
            }
            catch (MySqlException e)
            {
                throw new MonException(er.MessageUtilisateur(), er.MessageApplication(),
                e.Message);
            }
        }
    }
}
